#include <iostream>

using namespace std;
#define TRUE 1
#define ERROR 0
#define OK 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct Lnode {
    ElemType data;
    struct Lnode *next;
} Lnode, *LinkList;

LinkList ScoreList = NULL;


void printfInfo();

void beginCreateList();

void printfScoreList();

void insertToScoreList();

Lnode *previousLode(int n);

void deleteToScoreList();

Lnode *returnAccurateLnodeP(const char *info = "please in index", int flag = 1);

void Totalnumberqueries();

int ScoreListLength();

void GetIndexToScoreList();

Lnode *IndexLnode(int n);

int main() {
    do {
        printfInfo();
        int flag;
        cin >> flag;
        switch (flag) {
            case 0:
                exit(0);
            case 1:
                beginCreateList();
                break;
            case 2:
                insertToScoreList();
                break;
            case 3:
                deleteToScoreList();
                break;
            case 4:
                GetIndexToScoreList();
                break;
            case 5:
                break;
            case 6:
                Totalnumberqueries();
                break;
        }
    } while (1);
    return 0;
}


void printfInfo() {
    cout << "*********************Grades management*************" << endl;
    cout << "*********************1.create List*****************" << endl;
    cout << "*********************2.insert List*****************" << endl;
    cout << "*********************3.delete List*****************" << endl;
    cout << "*********************4.Serial number query*********" << endl;
    cout << "*********************5.Serial Value query**********" << endl;
    cout << "*********************6.Total number of queriy******" << endl;
    cout << "*********************0.exit************************" << endl;
}

void beginCreateList() {
    cout << "num is :" << endl;
    int num;
    cin >> num;
    ScoreList = new Lnode;
    Lnode *tail = ScoreList;
    cout << "insert in to score" << endl;
    while (num--) {
        Lnode *s = new Lnode;
        cin >> s->data;
        tail->next = s;
        tail = s;
    }
    tail->next = NULL;
    printfScoreList();
}

void printfScoreList() {
    if (!ScoreList)return;
    Lnode *s = ScoreList->next;
    while (s) {
        cout << s->data << " ";
        s = s->next;
    }
    cout << endl;
}

void insertToScoreList() {
    int num;
    cout << "please in score " << endl;
    cin >> num;
    Lnode *p = returnAccurateLnodeP();
    Lnode *s = new Lnode;
    s->data = num;
    p->next = s;
    s->next = NULL;
    printfScoreList();
}

Lnode *previousLode(int n) {
    Lnode *p = ScoreList;
    int j = 0;
    while (p && j < n - 1) {
        p = p->next;
        j++;
    }
    if (p) return p;
    else return NULL;
}

void deleteToScoreList() {
    Lnode *p = returnAccurateLnodeP();
    while (!p->next) {
        p = returnAccurateLnodeP("index It's not legal,please remake in");
    }
    Lnode *s = p->next;
    p->next = s->next;
    cout << "delete data is " << s->data << endl;
    delete s;
}


Lnode *returnAccurateLnodeP(const char *info, int flag) {
    int index;
    cout << info << endl;
    cin >> index;
    if (flag) {
        while (index <= 0) {
            cout << "index It's not legal,please remake in" << endl;
            cin >> index;
        }
        Lnode *p = previousLode(index);
        while (!p) {
            cout << "index It's not legal,please remake in" << endl;
            cin >> index;
            p = previousLode(index);
        }
        return p;
    } else {
        Lnode *p = IndexLnode(index);
        if (!p) return NULL;
        return index <= 0 ? NULL : p;
    }
}

void Totalnumberqueries() {
    cout << "ScoreList is " << ScoreListLength() << endl;
    printfScoreList();
}

int ScoreListLength() {
    if (!ScoreList)return 0;
    Lnode *s = ScoreList->next;
    int j = 0;
    while (s) {
        s = s->next;
        j++;
    }
    return j;
}

void GetIndexToScoreList() {
    Lnode *p = returnAccurateLnodeP("please searchIndex into", 0);
    if (p) {
        cout << p->data << endl;
    } else {
        cout << -1 << endl;
    }
}

Lnode *IndexLnode(int n) {
    if (!ScoreList)return NULL;
    Lnode *s = ScoreList->next;
    while (--n && s) {
        s = s->next;
    }
    return s ? s : NULL;
}